package com.weibo.sina.mywebview;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private WebView webView;
    private TextView back,home;
    private long minutes = 30*1000;
    private long countDown = 0;
    private Handler backToHomeHandler=new Handler();
    private BackToHomeHandlerThread thread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        thread = new BackToHomeHandlerThread();
        super.onCreate(savedInstanceState);
        int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //获得当前窗体对象
        Window window=MainActivity.this.getWindow();
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
        setContentView(R.layout.activity_main);
        back= (TextView) findViewById(R.id.back);
        home= (TextView) findViewById(R.id.home);
        init();
        resetThread();
    }

    private void init() {
        webView = (WebView) findViewById(R.id.webview);
        //WebView加载web资源
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String
                    url) {
                view.loadUrl(url); // 根据传入的参数再去加载新的网页
                return true; // 表示当前WebView可以处理打开新网页的请求，不用借助系统浏览器

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                resetThread();
            }
        });

        webView.loadUrl("http://47.90.8.33/mobile/test_items");
        webView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    resetThread();
                }
                return false;
            }
        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();// 返回前一个页面
            return true;
        }
        return false;
    }


    private void resetThread(){
        if(thread != null){
            backToHomeHandler.removeCallbacks(thread);
            thread = new BackToHomeHandlerThread();
            backToHomeHandler.postDelayed(thread,minutes);
        }
    }

    private void setListener(){
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                webView.goBack();
                resetThread();

            }
        });
        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,MainActivity.class));
                finish();
            }
        });
    }



    public class BackToHomeHandlerThread implements Runnable {

        @Override
        public void run() {
            startActivity(new Intent(MainActivity.this,MainActivity.class));
            finish();
        }
    }

}
